home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / snpd1292.zip / UUENCODE.C < prev    next >
C/C++ Source or Header  |  1992-12-26  |  3KB  |  147 lines

  1. /* uuencode.c */
  2.  
  3. /*
  4. uudecode and uuencode are easily implemented under MSDOS as well.  Here
  5. are the sources for Microsoft C v3.0, but if you have another kind of C
  6. compiler, there should be perhaps only 1 change -- the output file of
  7. uudecode and the input file of uuencode must be in binary format.
  8. (ie.  binary files, like .EXE files may have byte patterns that are the
  9. same as ^Z, which signals end-of-file in non-binary (text) mode).
  10.  
  11.     Don Kneller
  12. UUCP:   ...ucbvax!ucsfcgl!kneller
  13. ARPA:   kneller@ucsf-cgl.ARPA
  14. BITNET: kneller@ucsfcgl.BITNET
  15.  
  16.    patched up for BC++ 3.1 by Alan Eldridge 10/12/92
  17.        (UUCP: alane@wozzle.linet.org, FIDO: 1:272/38.473)
  18.  
  19. */
  20.  
  21. #ifndef lint
  22. #ifndef MSDOS
  23. static char sccsid[] = "@(#)uuencode.c  5.1 (Berkeley) 7/2/83";
  24. #endif
  25. #endif
  26.  
  27. /*
  28.  * uuencode [input] output
  29.  *
  30.  * Encode a file so it can be mailed to a remote system.
  31.  */
  32.  
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <sys/types.h>
  36. #include <sys/stat.h>
  37.  
  38. /* ENC is the basic 1 character encoding function to make a char printing */
  39.  
  40. #define ENC(c) (((c) & 077) + ' ')
  41.  
  42. void encode(FILE *in, FILE *out);
  43. void outdec(char *p, FILE *f);
  44. int fr(FILE *fd, char *buf, int cnt);
  45.  
  46. main(int argc, char *argv[])
  47. {
  48.       FILE *in;
  49.       struct stat sbuf;
  50.       int mode;
  51.  
  52.       /* optional 1st argument */
  53.  
  54.       if (argc > 2)
  55.       {
  56. #ifdef MSDOS
  57.             /* Use binary mode */
  58.             if ((in = fopen(argv[1], "rb")) == NULL)
  59.             {
  60. #else
  61.             if ((in = fopen(argv[1], "r")) == NULL)
  62.             {
  63. #endif
  64.                   perror(argv[1]);
  65.                   exit(1);
  66.             }
  67.             argv++; argc--;
  68.       }
  69.       else  in = stdin;
  70.  
  71.       if (argc != 2)
  72.       {
  73.             printf("Usage: uuencode [infile] remotefile\n");
  74.             exit(2);
  75.       }
  76.  
  77.       /* figure out the input file mode */
  78.  
  79.       fstat(fileno(in), &sbuf);
  80.       mode = sbuf.st_mode & 0777;
  81.       printf("begin %o %s\n", mode, argv[1]);
  82.  
  83.       encode(in, stdout);
  84.  
  85.       printf("end\n");
  86.       return 0;
  87. }
  88.  
  89. /*
  90.  * copy from in to out, encoding as you go along.
  91.  */
  92.  
  93. void encode(FILE *in, FILE *out)
  94. {
  95.       char buf[80];
  96.       int i, n;
  97.  
  98.       for (;;)
  99.       {
  100.             /* 1 (up to) 45 character line */
  101.  
  102.             n = fr(in, buf, 45);
  103.             putc(ENC(n), out);
  104.  
  105.             for (i = 0; i < n; i += 3)
  106.                   outdec(&buf[i], out);
  107.  
  108.             putc('\n', out);
  109.             if (n <= 0)
  110.                   break;
  111.       }
  112. }
  113.  
  114. /*
  115.  * output one group of 3 bytes, pointed at by p, on file f.
  116.  */
  117.  
  118. void outdec(char *p, FILE *f)
  119. {
  120.       int c1, c2, c3, c4;
  121.  
  122.       c1 = *p >> 2;
  123.       c2 = ((p[0] << 4) & 060) | ((p[1] >> 4) & 017);
  124.       c3 = ((p[1] << 2) & 074) | ((p[2] >> 6) & 03);
  125.       c4 = p[2] & 077;
  126.       putc(ENC(c1), f);
  127.       putc(ENC(c2), f);
  128.       putc(ENC(c3), f);
  129.       putc(ENC(c4), f);
  130. }
  131.  
  132. /* fr: like read but stdio */
  133.  
  134. int fr(FILE *fd, char *buf, int cnt)
  135. {
  136.       int c, i;
  137.  
  138.       for (i = 0; i < cnt; i++)
  139.       {
  140.             c = getc(fd);
  141.             if (c == EOF)
  142.                   return(i);
  143.             buf[i] = c;
  144.       }
  145.       return (cnt);
  146. }
  147.